import {config} from "../config";

export function get(url, param) {
    const params = new URLSearchParams();
    for (const key in param) {
        if (param.hasOwnProperty(key)) {
            params.append(key, param[key]);
        }
    }
    return ajax(url + "?" + params.toString(), {method: "GET"});
}

export function post(url, param) {
    if (param) {
        const params = new URLSearchParams();
        for (const key in param) {
            if (param.hasOwnProperty(key)) {
                params.append(key, param[key]);
            }
        }
        return ajax(url, {body: params, method: "POST"});
    } else {
        return ajax(url, {method: "POST"});
    }
}

export async function ajax(url, option) {
    let method=config.serverUrl+url;
    console.log(method);
    console.log(option);
    const response = await fetch(method, option);
    console.log(response);
    return response;
}